package com.zmn.oms.admin.controller.report;

import com.google.common.collect.Lists;
import com.zmn.common.utils.office.ExcelUtil;
import com.zmn.oms.business.interfaces.log.OrderLogBService;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.model.dto.log.OperatorAchievementsDto;
import com.zmn.oms.model.dto.log.OperatorAchievementsQuery;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 类描述：
 *
 * @author LiangHailong
 * @date 2019/5/7 11:23
 */
@Controller
@RequestMapping("/operator/achievement")
public class OperatorAchievementsController {
    private static final Logger logger = LoggerFactory.getLogger(OperatorAchievementsController.class);

    @Autowired
    OrderLogBService orderLogBService;

    @RequestMapping(value = "/export")
    public void export(HttpServletResponse response, @Valid OperatorAchievementsQuery query) throws IOException {
        List<OperatorAchievementsDto> results = Lists.newArrayList();
        // orderLogBService.findOperatorAchievements(
        //         query.getStartTime(), query.getEndTime(), query.getType(), false);

        String orderLogTypeName = "操作员绩效-" + OrderLogConsts.getOrderLogTypeName(query.getType());

        XSSFWorkbook workbook = this.getXSSFWorkbookFromList(results, orderLogTypeName);

        OutputStream outputStream = response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename="
                + URLEncoder.encode("操作员绩效-" + orderLogTypeName + ".xls", "UTF-8"));
        response.setContentType("application/msexcel");
        //表格导出
        workbook.write(outputStream);
        outputStream.close();
    }

    private XSSFWorkbook getXSSFWorkbookFromList(List<OperatorAchievementsDto> results, String orderLogTypeName) {
        List<List> data = new ArrayList<>(results.size());
        // 表头
        List<String> columns = new ArrayList<>(3);
        columns.add("操作员Id");
        columns.add("操作员");
        columns.add(orderLogTypeName);
        data.add(columns);
        // 数据
        for (int i = 0, len = results.size(); i < len; i++) {
            OperatorAchievementsDto item = results.get(i);

            List<Object> row = new ArrayList<>(3);
            row.add(item.getOperatorId());
            row.add(item.getOperator());
            row.add(item.getAchievements());

            data.add(row);
        }

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("操作员绩效-" + orderLogTypeName);

        ExcelUtil.parseList(data, sheet);

        return workbook;
    }
}
